<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="检索信息" prop="search">
        <el-input v-model="queryParams.search" placeholder="请输入检索信息" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="活动名称" prop="categoryId">
        <el-select v-model="queryParams.categoryId" placeholder="请选择活动名称">
          <el-option v-for="item in actNameList" :label="item.actTitle" :value="item.actId" :key="item.actId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="examineStatus">
        <el-select v-model="queryParams.examineStatus" placeholder="审核状态">
          <el-option label="通过" value="0"></el-option>
          <el-option label="驳回" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报名时间" prop="createTime">
        <el-date-picker clearable v-model="queryParams.createTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['manage:join:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="joinList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动名称" align="center" prop="actName" />
      <el-table-column label="报名姓名" align="center" prop="opName" />
      <el-table-column label="联系电话" align="center" prop="opPhone" />
      <el-table-column label="活动状态" align="center" prop="opStatus">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.opStatus == '0'">
            火热报名中
          </el-tag>
          <el-tag effect="dark" v-else>
            活动结束
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="examineStatus">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.examineStatus == '0'">
            审核通过
          </el-tag>
          <el-tag effect="dark" v-else-if="scope.row.examineStatus == '1'">
            驳回
          </el-tag>
          <el-tag effect="dark" v-else>
            审核中
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" align="center" prop="examineRemark">
        <template slot-scope="scope">
          <el-tag effect="dark" type="danger" v-if="scope.row.examineRemark == null">
            暂无审核
          </el-tag>
          <el-tag effect="dark" v-else>
            {{ scope.row.examineRemark }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="报名时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.examineStatus != '0'" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['manage:join:edit']">审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['manage:join:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改活动参与对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核" prop="examineStatus">
          <el-select v-model="form.examineStatus" placeholder="审核状态">
            <el-option label="通过" value="0"></el-option>
            <el-option label="驳回" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核意见" prop="examineRemark">
          <el-input type="textarea" :rows="2" v-model="form.examineRemark" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listJoin, getJoin, delJoin, addJoin, updateJoin } from "@/api/manage/join";
import { getOnLineActiveList } from "@/api/manage/activity"
export default {
  name: "Join",
  data () {
    return {
      // 活动集合
      actNameList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 活动参与表格数据
      joinList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        search: null,
        categoryId: null,
        examineStatus: null,
        createTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created () {
    this.getList();
    this.getActivityListOnLine();
  },
  methods: {
    // 获取未下假活动
    getActivityListOnLine () {
      getOnLineActiveList().then(res => {
        this.actNameList = res.data
      })
    },
    /** 查询活动参与列表 */
    getList () {
      this.loading = true;
      listJoin(this.queryParams).then(response => {
        this.joinList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        jaId: null,
        actId: null,
        actName: null,
        opId: null,
        opName: null,
        opPhone: null,
        createTime: null,
        opStatus: null,
        delFlag: null,
        examineStatus: null,
        examineRemark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.jaId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加活动参与";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const jaId = row.jaId || this.ids
      getJoin(jaId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "审核意见";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.jaId != null) {
            updateJoin(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addJoin(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const jaIds = row.jaId || this.ids;
      this.$modal.confirm('是否确认删除活动参与编号为"' + jaIds + '"的数据项？').then(function () {
        return delJoin(jaIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('manage/join/export', {
        ...this.queryParams
      }, `join_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
